<script type="text/javascript">
	Yutil.useLoadingMessage();
	Yutil.en.enhance();
</script>
<%
	//session id 
	String userid = (String) session.getValue("user");
	List personList = PersonService.$.getPersons(userid);
	
	UIutil $ = UIutil.$;
%>
<div style="height: 6%">
	<table align='left'>
		<tr>
			<td>
			<!-- 
			<%=JspWidget.$.actionButton("创建用户", "la_person_create.jsp",
							null)%>
			 -->
				<button accessKey="c" onclick="actionPage('la_person_create.jsp')"><u>创</u>建用户</button>
			</td>
			<td>
				<button accessKey="s" onclick="deletePersons()"><u>删</u>除用户</button>
			</td>
		</tr>
	</table>
</div>
<p></p>
<fieldset>
	<legend>
		用户列表
	</legend>
	<table align="left" border="1px">
		<tr>
			<th>
				<font>Filter</font>
			</th>
			<td>
				<select id="personType" class="required" name="personType"
					onchange="filterPersonByType(this)">
					<option value="---">
						---
					</option>
					<%
						List accessGroups = (List) AccessGroupService.$.getGroups();
						for (int i = 0; i < accessGroups.size(); i++) {
					%>
					<option value="<%=accessGroups.get(i)%>"><%=accessGroups.get(i)%></option>
					<%
						}
					%>
				</select>
			</td>
		</tr>
	</table>
	<table align="left" border="1px" class="grid">
		<thead>
			<tr>
				<th>
					<input type="checkbox" id="selAll" onclick="selectAll(this)" />
				</th>
				<th>
					用户组
				</th>
				<th>
					用户名
				</th>
				<th>
					密码
				</th>
				<th>
					姓名
				</th>
				<th>
					联系方式
				</th>				
				<th>
					行为
				</th>
			</tr>
		</thead>
		<tbody id="persons">
			<%
				for (int i = 0; i < personList.size(); i++) {
					Person person = (Person) personList.get(i);
					String personType = PersonService.$.getPersonType(person);
					String personTypeCode = "";
					if (LocalContants.PERSON_TYPE_S.equalsIgnoreCase(personType))
						personTypeCode = "S";
					else if (LocalContants.PERSON_TYPE_M
							.equalsIgnoreCase(personType))
						personTypeCode = "M";
					else if (LocalContants.PERSON_TYPE_M
							.equalsIgnoreCase(personType))
						personTypeCode = "O";
			%>
			<tr personTypeCode="<%=personTypeCode%>" personType="<%=personType%>"
				type="person">
				<td>
					<input type="checkbox" name="checkbox"
						value="<%=person.getUserid()%>" onclick="cascadeAll(this)" />
				</td>
				<td>
					<%=personType%></td>
				<td>
					<a href="javascript:;" onclick="editPerson(this)"
						value="<%=person.getUserid()%>"><%=person.getUserid()%></a>
				</td>
				<td>
					<acronym><%=person.getPassword()%>
						<span name="personSpan" class="click_popup" style="width:3em">
							<a href="javascript:;" onclick="editPerson(this)"
								value="<%=person.getUserid()%>"><%=person.getUserid()%></a>
						</span>
					</acronym>
				</td>
				<td><%=$.enc(person.getName())%></td>
				<td><a href="javascript:void(0);"><%=$.enc(person.getEmail())%></a></td>
				<td>
					<a href="javascript:;" onclick="deletePerson(this)"
						id="<%=person.getUserid()%>">删除</a>
				</td>
			</tr>
			<%
				}
			%>
		</tbody>
	</table>
</fieldset>

<script type="text/javascript">
		editPerson = function(oElem) {
				Yutil.createDialog('personDialog', '80%', '80%', '修改用户信息');
				var aLink = oElem;
				var userid = oElem.getAttribute('value');
				var url = 'la_person_edit.jsp';
				var pars = "userid=" + userid;
				var myAjax = new Ajax.Updater('personDialog_content', url, {method:'get', parameters:pars, evalScripts: true});
				Yutil.showDialog('personDialog');
		}
		actionPage = function(dest) {
				var url = dest;
				var myAjax = new Ajax.Updater('right',url,{method:'get',evalScripts: true});
		}
		
		deletePerson = function(oElem) {
				var userid = oElem.id;
				var pars = "deleteUser=" + userid;
				var url = "/" + "<%=LocalContants.PROJECT_NAME%>" + "/personDelete.do";
				var myAjax = new Ajax.Updater('right',url,{method:'get',parameters: pars,evalScripts: true});
		}
		
		deletePersons = function() {
				var checkboxes = document.getElementsByName("checkbox");
				var count = 0;
				var personArr = [];
				for(var i=0;i<checkboxes.length;i++) {
						var checkb = checkboxes[i];
						if(checkb.checked) {
								count++;
								personArr.push(checkb.value);
						}
				}
				var userids="";
				if(count == 0) {
						alert("Please a person at least to delete. ");
						return;
				} else {
						for(var i=0;i<personArr.length;i++) {
								if(i < personArr.length - 1) {
										userids += personArr[i] + ";" ;
								} else {
										userids += personArr[i] ;
								}
						} 
				}
				var pars = "deleteUser=" + userids;
				var url = "/" + "<%=LocalContants.PROJECT_NAME%>" + "/personDelete.do";
				var myAjax = new Ajax.Updater('right',url,{method:'get',parameters: pars,evalScripts: true});
		}
		
		filterPersonByType = function(oElem) {
				var sel = oElem;
				var trs = jQuery("tr[personType=" + sel.value + "]");
				var allTrs = jQuery("tr[@type='person']");
				for(var i=0;i<allTrs.length;i++){
					var eachTr = allTrs[i];
					if(eachTr.getAttribute("personType") == sel.value || (sel.value == '---'))
						eachTr.style.display = '';
					else
						eachTr.style.display = 'none';
				}
		}
		
		selectAll = function(oElem) {
				var checked = oElem.checked;
				var checkboxes = document.getElementsByName("checkbox");
				for(var i=0;i<checkboxes.length;i++) {
						var checkb = checkboxes[i];
						checkb.checked = checked;
				}
		}
		
		cascadeAll = function(oElem) {
				var checked = oElem.checked;
				if(!checked) {
						$('selAll').checked = checked;
				} 
		}
</script>

<%@ page import="java.util.*"%>
<%@ page import="com.esms.bean.Person"%>
<%@ page import="com.esms.struts.service.*"%>
<%@ page language="java" contentType="text/html; charset=utf-8"%>